package com.tim.source.templete;

import java.io.InputStream;
import java.util.List;

import com.tim.source.MakeSource;
import com.tim.source.common.CommonInfo;
import com.tim.source.common.FieldInfo;
import com.tim.source.common.TableInfo;
import com.tim.source.util.StringUtil;

/**
 * 数据表转Entity.
 * 
 * @author Tim.
 */
public class TableToEntity {
	// entity类名称.
	public static String className;
	// entity完整类名
	public static String fullClassName;
	// entity类模板
	private static String TEMPLETE;
	// field模板
	private static String FIELD_TEMPLETE;
	// setter模板
	private static String SETTER_TEMPLETE;
	// getter模板
	private static String GETTER_TEMPLETE;
	static {
		// 获取Entity模板
		TEMPLETE = getPartTemplete("entity");
		// 替换package
		TEMPLETE = TEMPLETE.replace("%package%", StringUtil
				.getPackage(MakeSource.ENTITY));
		// 替换作者
		TEMPLETE = TEMPLETE.replace("%author%", CommonInfo.AUTHOR);
		// 替换时间
		TEMPLETE = TEMPLETE.replace("%dateTime%", CommonInfo.DATESTRING);

		// 获取Field模板
		FIELD_TEMPLETE = getPartTemplete("field");
		// 获取Setter模板
		SETTER_TEMPLETE = getPartTemplete("setter");
		// 获取Getter模板
		GETTER_TEMPLETE = getPartTemplete("getter");
	}

	/**
	 * 获取指定部位模板.
	 * 
	 * @param part
	 * @return
	 */
	private static String getPartTemplete(String part) {
		try {// 获取setter模板.
			InputStream input = TableToIDao.class.getResourceAsStream(part
					+ ".tmp");
			byte[] data = new byte[input.available()];
			input.read(data);
			input.close();
			return new String(data, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String execute() {
		// 替换类名称
		String tempStr = TEMPLETE.replace("%className%", TableInfo.className);
		// 替换import
		StringBuilder importStr = new StringBuilder();
		// 替换属性定义
		StringBuilder fieldStr = new StringBuilder();
		// 替换setter&getter
		StringBuilder methodStr = new StringBuilder();
		List<FieldInfo> fieldList = TableInfo.fieldList;
		for (int i = 0; i < fieldList.size(); i++) {
			// 获取表对应字段信息
			FieldInfo fieldInfo = fieldList.get(i);
			// 添加import语句
			if (!fieldInfo.isLangPackage()
					&& importStr.indexOf(fieldInfo.getFullDataType()) == -1) {
				importStr.append("import " + fieldInfo.getFullDataType()
						+ ";\r\n");
			}
			// 添加属性定义
			String temp = FIELD_TEMPLETE.replace("%dataType%", fieldInfo
					.getDataType());
			temp = temp.replace("%field%", fieldInfo.getPropName());
			fieldStr.append(temp);
			// 添加setter&getter
			temp = SETTER_TEMPLETE + GETTER_TEMPLETE;
			temp = temp.replace("%dataType%", fieldInfo.getDataType());
			temp = temp.replace("%method%", fieldInfo.getMethod());
			temp = temp.replace("%field%", fieldInfo.getPropName());
			methodStr.append(temp);
		}
		// 替换import语句
		tempStr = tempStr.replace("%importList%", importStr.toString());
		// 替换属性定义
		tempStr = tempStr.replace("%filedList%", fieldStr.toString());
		// 替换setter&getter
		tempStr = tempStr.replace("%setter&getter%", methodStr.toString());
		return tempStr;
	}
}
